package com.android.common.mvp.view.fragment;

import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;
import com.android.common.R;
import com.android.common.base.BaseFragment;
import com.android.common.base.BaseFragmentAdapter;
import com.android.common.constant.Constant;
import com.android.common.constant.IntentKey;
import com.android.common.mvp.model.MediaItem;
import java.util.ArrayList;
import java.util.List;

public class PreviewViewPagerFragment extends BaseFragment {

    private ViewPager2 mVpPreview;

    private List<MediaItem> mMediaItems;

    private int mCurrentItem;

    @Override
    protected int onSetRootViewId() {
        return R.layout.fragment_preview_view_pager;
    }

    @Override
    protected void onFindView() {
        super.onFindView();
        mVpPreview = mRootView.findViewById(R.id.vp_preview);
    }

    @Override
    protected void onInitView() {
        super.onInitView();
        mMediaItems = mBundle.getParcelableArrayList(IntentKey.MEDIA_ITEM_KEY);
        mCurrentItem = mBundle.getInt(IntentKey.CURRENT_ITEM_KEY,Constant.ZERO);
        if (null != mMediaItems && !mMediaItems.isEmpty()){
            List<Fragment> fragments = new ArrayList<>();
            for (MediaItem mediaItem : mMediaItems){
                PicturePreviewFragment previewFragment = new PicturePreviewFragment();
                previewFragment.setMediaItem(mediaItem);
                fragments.add(previewFragment);
            }

            BaseFragmentAdapter mFragmentStateAdapter = new BaseFragmentAdapter(this, fragments);
            mVpPreview.setAdapter(mFragmentStateAdapter);
            mVpPreview.setCurrentItem(mCurrentItem,false);
        }
    }


    @Override
    protected void onInitEvent() {
        super.onInitEvent();
    }

    @Override
    protected boolean onNeedToolBar() {
        return false;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
